package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.SysDictType;
import com.itheima.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author xuchangshuo
 * @date 2020/8/3 20:23
 */

@CrossOrigin
@RequestMapping("/sysDictType")
@RestController
public class SysDictTypeController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    //添加字典类型
    @RequestMapping("/add")
    public Result add(@RequestBody Map<String, Object> sysDictTypeVO) {
        sysDictTypeService.add(sysDictTypeVO);
        return new Result("success", "添加成功");
    }

    //批量删除字典类型
    @RequestMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Map<String, Object>> sysDictTypeVoList) {
        try {
            sysDictTypeService.deleteBatch(sysDictTypeVoList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("faild", "批量删除失败");
        }

        return new Result("success", "批量删除成功");
    }

    //编辑字典类型
    @RequestMapping("/edit")
    public Result edit(@RequestBody Map<String, Object> sysDictTypeVO) {
        try {
            sysDictTypeService.edit(sysDictTypeVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("faild", "编辑失败");
        }
        return new Result("success", "编辑成功");

    }

    //获取字典类型列表
    @RequestMapping("/getList")
    public Result getList(@RequestBody Map<String, Object> sysDictTypeVO) {
        Page<SysDictType> sysDictTypeList = (Page<SysDictType>) sysDictTypeService.getList(sysDictTypeVO);
        PageResult<SysDictType> pageResult = new PageResult<>();
        pageResult.setRecords(sysDictTypeList);
        pageResult.setTotal(sysDictTypeList.getTotal());
        pageResult.setCurrent((long) sysDictTypeList.getPageNum());
        pageResult.setOptimizeCountSql(true);
        pageResult.setSearchCount(true);
        return new Result("success", pageResult);
    }
}
